﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace CadClient.CadHelpers
{
    public static class CreateLineHelper
    {
        public static List<Line> CreateLine(this Document doc)
        {
            var db = doc.Database;
            var ed = doc.Editor;
            var lineList = new List<Line>();

            Line line = new Line();

            var pointStart = new Point3d(100, 100, 0);
            var pointPre = pointStart;
            var ppr = ed.GetPoint2("\n选择第一个点：");
            if (ppr.Status == PromptStatus.Cancel) return null;
            if (ppr.Status == PromptStatus.None) pointPre = pointStart;
            if (ppr.Status == PromptStatus.OK)
            {
                pointStart = ppr.Value;
                pointPre = pointStart;
            }
            bool isContinue = true;
            while (isContinue)
            {
                if (lineList.Count > 1)
                    ppr = ed.GetPoint("\n 指定下一个点或[闭合(C)/放弃(U):]", pointPre, new string[] { "C", "U" });
                else
                    ppr = ed.GetPoint("\n 指定下一点或[放弃U]:", pointPre, new string[] { "U" });
                var pointNext = new Point3d();
                if (ppr.Status == PromptStatus.Cancel) return null;
                if (ppr.Status == PromptStatus.None) return null;
                if (ppr.Status == PromptStatus.OK)
                {
                    pointNext = ppr.Value;
                    line = db.AddLineToModelSpace1(pointPre, pointNext);
                    lineList.Add(line);

                    pointPre = pointNext;
                }
                if (ppr.Status == PromptStatus.Keyword)
                {
                    switch (ppr.StringResult)
                    {
                        case "U":
                            if (lineList.Count == 0)
                            {
                                pointStart = new Point3d(100, 100, 0);
                                pointPre = pointStart;
                                ppr = ed.GetPoint2("\n 指定一个点：");
                                if (ppr.Status == PromptStatus.Cancel) return null;
                                if (ppr.Status == PromptStatus.None) pointPre = pointStart;
                                if (ppr.Status == PromptStatus.OK)
                                {
                                    pointStart = ppr.Value;
                                    pointPre = pointStart;
                                }

                            }
                            else if (lineList.Count > 0)
                            {
                                int count = lineList.Count;
                                var line2 = lineList.ElementAt(count - 1);
                                pointPre = line2.StartPoint;
                                lineList.RemoveAt(count - 1);
                                //删除
                                line2.Erase(true);
                            }
                            break;
                        case "C":
                            line = db.AddLineToModelSpace1(pointStart, pointPre);
                            lineList.Add(line);
                            isContinue = false;
                            break;
                        case " ":
                            isContinue = false;
                            break;
                        default:
                            break;
                    }
                }
            }

            return lineList;
        }
    }
}
